<template>
  <div class="member-order">
    <!-- Tab组件-->
    <XtxTabs v-model="activeName" @tab-click="changeTab">
      <XtxTabsPanel
        v-for="item in orderStatus"
        :key="item.name"
        :label="item.label"
        :name="item.name"
      >{{ item.label }}
      </XtxTabsPanel
      >
    </XtxTabs>
    <!--  订单列表-->
    <div class="order-list" v-if="orderList">
      <div v-if="loading" class="loading"></div>
      <div v-if="!loading&&total === 0" class="none">暂无数据</div>
      <OrderItem v-for="item in orderList"
                 :key="item.id" :order="item"
                 @on-cancel-order="handleCancel"
                 @on-delete-order="handleDelete"
                 @on-confirm-order="handleConfirmOrder"
                 @on-logistics-order="handleLogistics"
      />
    </div>
    <!--  分页组件-->
    <XtxPagination v-if="total>0"
                   :current-page="reqParams.page"
                   :page-size="reqParams.pageSize"
                   :total="total"
                   @current-page="reqParams.page=$event"
    />
    <!--   取消原因组件-->
    <OrderCancel ref="orderCancelCom"/>
    <!-- 查看物流组件-->
    <OrderLogistics ref="orderLogisticsCom"/>
  </div>
</template>

<script>
import XtxTabs from '@/components/library/xtx-tabs'
import XtxTabsPanel from '@/components/library/xtx-tabs-panel'
import { reactive, ref, watch } from 'vue'
import OrderItem from '@/views/member/order/components/order-item'
import { confirmOrder, deleteOrder, findOrderList } from '@/api/order'
import { orderStatus } from '@/api/constants'
import OrderCancel from '@/views/member/order/components/order-cancel'
import Confirm from '@/components/library/Confirm'
import Message from '@/components/library/Message'
import OrderLogistics from '@/views/member/order/components/order-logistics'

export default {
  name: 'MemberOrder',
  components: {
    OrderLogistics,
    OrderCancel,
    OrderItem,
    XtxTabsPanel,
    XtxTabs
  },
  setup () {
    // 选中得名字
    const activeName = ref('all')
    // 选项卡发生改变
    const changeTab = ({ index }) => {
      reqParams.page = 1
      reqParams.orderState = index
    }
    // 获取我的订单数据
    const reqParams = reactive({
      page: 1,
      pageSize: 10,
      // 订单状态，1为待付款、2为待发货、3为待收货、4为待评价、5为已完成、6为已取消，未传该参数或0为全部
      orderState: 0
    })
    const orderList = ref([])
    const loading = ref(false)
    const total = ref(0)
    const getOrderList = () => {
      loading.value = true
      findOrderList(reqParams).then(data => {
        orderList.value = data.result.items
        total.value = data.result.counts
        loading.value = false
      })
    }
    watch(reqParams, () => {
      getOrderList()
    }, { immediate: true })
    // 删除订单
    const handleDelete = (order) => {
      Confirm({ text: '亲，您确认删除该订单吗？' }).then(() => {
        deleteOrder([order.id]).then(() => {
          Message({
            type: 'success',
            text: '删除成功'
          })
          getOrderList()
        })
      }).catch(e => {})
    }
    return {
      activeName,
      orderList,
      loading,
      reqParams,
      total,
      orderStatus,
      changeTab,
      handleDelete,
      ...useCancelOrder(),
      ...useConfirmOrder(),
      ...useLogistics()
    }
  }
}
// 取消订单逻辑
export const useCancelOrder = () => {
  const orderCancelCom = ref(null)
  const handleCancel = (order) => {
    orderCancelCom.value.open(order)
  }
  return {
    orderCancelCom,
    handleCancel
  }
}

// 确认收获逻辑
export const useConfirmOrder = () => {
  const handleConfirmOrder = (order) => {
    confirmOrder(order.id).then(data => {
      Message({
        type: 'success',
        text: '确认收货成功'
      })
      order.orderState = 4
    })
  }
  return { handleConfirmOrder }
}
// 查看物流逻辑
export const useLogistics = () => {
  const orderLogisticsCom = ref(null)
  const handleLogistics = (order) => {
    orderLogisticsCom.value.open(order)
  }
  return {
    orderLogisticsCom,
    handleLogistics
  }
}
</script>

<style scoped lang="less">

.member-order {
  background: #fff;
  padding: 20px;
}

.order-list {
  padding: 20px;
  position: relative;
  min-height: 400px;
}

.loading {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background: rgba(255, 255, 255, .9) url(../../../assets/images/loading.gif) no-repeat center;
}

.none {
  height: 400px;
  text-align: center;
  line-height: 400px;
  color: #999;
}

</style>
